package com.market.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.market.dao.ProductThemeDao;
import com.market.domain.Product;
import com.market.domain.ProductPrice;
import com.market.domain.ProductTheme;
import com.market.domain.Weekly;

@Repository("productThemeDao")
public class ProductThemeDaoJpa extends GenericDaoJpa<ProductTheme, Long>
		implements ProductThemeDao {
	
	
	public ProductThemeDaoJpa(){
		super(ProductTheme.class);
	}

	public void remove(ProductTheme theme) {
		if (this.findById(theme.getId()) == null) {
			log.debug(this.getClass() + " Product theme with id " + theme.getId()
					+ " is not found, can not be deleted");
		} else {
			log.debug(this.getClass() + " Product theme  with id " + theme.getId()
					+ " is found, will be deleted");
			super.removeById(theme.getId());
		}

	}

	public List<Product> findProducts(ProductTheme theme) {
		String sql = "select product.* from product, producttheme_product where "
				+ " producttheme_product.products_id=product.id and producttheme_product.producttheme_id= '"	+ theme.getId() + "'";
		Query query = this.em.createNativeQuery(sql, Product.class);
		List<Product> products = query.getResultList();
		return products;
	}

	public List<ProductTheme> findActiveProductThemes() {
		String sql = "select producttheme.* from producttheme where isActive=1 order by recommendIndex desc";
		Query query = this.em.createNativeQuery(sql, ProductTheme.class);
		List<ProductTheme> themes = query.getResultList();
		return themes;
	}
	
	
}
